import './public/index'
import './public/toastr'
import server from './date/url'

import {
  userName        as validate_loginName,
  pwd             as validate_password,
  identifyCode    as validate_identifyCode
} from './public/validate'

let app = new Vue({
  el: '#login-form',
  data: () => { // 设置成函数才可以调用$options.data()方法来清空数据
    return {
      loginName: '',
      password: '',
      identifyCode: '',
      img: ''
    }
  },
  mounted: function() { // 生命周期
    vue_mounted_valid()
    this.getValidImg()
  },
  methods: {
    getValidImg: function(e) {

      let that = this

      loading('获取验证码中')

      $.ajax({
        url: server,
        type: 'post',
        data: {
          task: 'getLoginValidImage',
          stamp: new Date().getTime()
        }
      })
      .done(function(res) {
        console.log("success")
        console.log(res)

        if (res.status === 'true' || res.status === true) {
          that.img = res.data.validImg
        } else {
          toastr.error(res.message || '')
        }

      })
      .fail(function(res) {
        console.log("error")
        console.log(res)
      })
      .always(function() {
        // console.log("complete")
        closeLoading()
      })

    },
    reg: function(e) {
      e.preventDefault()
      console.log('aaaaa')

      let that = this

      layer.msg('加载中', {
        icon: 16,
        shade: .6,
        time: 0
      })
      
      $.ajax({
        url: server,
        type: 'post',
        data: {
          task: '2reg',
          stamp: new Date().getTime()
        }
      })
      .done(function(res) {
        console.log("success")
        console.log(res)

        if (res.status === 'true' || res.status === true) {
          window.location = './sign.html'
        } else {
          toastr.error(res.message || '')
        }

      })
      .fail(function(res) {
        console.log("error")
        console.log(res)
      })
      .always(function() {
        layer.closeAll()
      })

      
    }
  }
})

function vue_mounted_valid () {
  let form_valid,
      form = $('#login-form')

  $.validator.setDefaults({ // 只能放在validate规则配置之前

    submitHandler: function() {    // 提交事件
      console.log('submit!')

      let $loadingToast = $('#loadingToast')

      $loadingToast.fadeIn(100)

      $.ajax({
        url: server,
        type: 'post',
        data: {
          task: 'inputLoginForm',
          stamp: new Date().getTime(),
          data: {
            user: $.trim(app.loginName),
            password: $.trim(app.password),
            valid: $.trim(app.identifyCode)
          }
        }
      })
      .done(function(res) {
        console.log(res)

        if (res.status === true || res.status === 'true') {
          console.log('登录成功')

          if (!res.data.status) {
            toastr.error('参数错误')

            return
          }
          
          if (res.data.status === 'wait') {
            window.location = './creditWait.html'
            
            return
          }
          
          if (res.data.status === 'fail') {
            window.location = './creditFail.html'
            
            return
          }
          
          if (res.data.status === 'success') {
            window.location = './creditGet.html'
            
            return
          }
          
          if (res.data.status === 'noApply') {
            window.location = './creditWithout.html'

            return
          }

          toastr.error('参数错误')

        } else {
          toastr.error(res.message || '')
          app.getValidImg()
        }

      })
      .fail(function(res) {

        console.log("error");
      })
      .always(function(res) {
        $loadingToast.fadeOut(100);
      })

    }
  })


  form_valid = form.validate({ // 验证
    rules: {
      loginName: validate_loginName,
      password: validate_password,
      identifyCode: validate_identifyCode
    }
  })

}

function findPassword() { // 弹窗
  let $getPwd       = $('#getPwd'),
      $getPwdDialog = $('#getPwdDialog'),
      $dialogBtn    = $('.weui-dialog__btn')

  $getPwd.on('click',(e) => {
    e.preventDefault();

    $getPwdDialog.fadeIn(200)
  })

  $dialogBtn.on('click',(e) => {
    e.preventDefault();

    let $el = $(e.target)
    $el.parents('.js_dialog').fadeOut(200)
  })
}

function footer() { // 解决移动端绝对定位的问题
  let h = $(window).height();

  $(window).resize(function() {

    if($(window).height() < h){
      $('.footer_bar').hide();
    }else{
      $('.footer_bar').show();
    }
  });
}

findPassword();
footer();


function loading(text) {
  layer.msg(text || '加载中', {
    icon: 16,
    shade: .6,
    time: 0
  })
}

function closeLoading() {
  layer.closeAll()
}




